package com.xyzq.cgate.admin.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.base.Preconditions;
import com.xyzq.cgate.admin.service.PluginService;
import com.xyzq.cgate.admin.wrapper.PluginWrapper;
import com.xyzq.cgate.common.constant.ZkPathConstants;
import com.xyzq.cgate.common.entity.Plugin;
import com.xyzq.cgate.common.response.ResponseEntity;
import com.xyzq.cgate.common.utils.IdUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenchen
 * @since 2019-06-06
 */
@RestController
@RequestMapping("/admin/plugin")
public class PluginController {

    @Autowired
    PluginService pluginService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Mono<ResponseEntity> list() {
        List<Plugin> list = pluginService.selectList(new EntityWrapper());
        list.stream().map(p -> PluginWrapper.wrapper(p))
                .collect(Collectors.toList());
        return Mono.just(new ResponseEntity<>().success(list));
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public Mono<ResponseEntity> create(@RequestBody Plugin plugin) {
        Preconditions.checkNotNull(plugin, "plugin为null");
        Preconditions.checkArgument(StringUtils.isNoneBlank(plugin.getName()));
        Preconditions.checkArgument(StringUtils.contains("01", plugin.getRole().toString())
                        && StringUtils.contains("01", plugin.getEnabled().toString()));
        plugin.setId(IdUtil.generateUUID());
        plugin.setDateCreated(new Timestamp(System.currentTimeMillis()));
        plugin.setDateUpdated(new Timestamp(System.currentTimeMillis()));
        if(pluginService.insertDbAndZk(plugin)) {
            return Mono.just(new ResponseEntity<>().success());
        } else {
            return Mono.just(new ResponseEntity<>().failure("insert fail"));
        }
    }


    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public Mono<ResponseEntity> delete(@RequestBody Plugin plugin) {
        Preconditions.checkNotNull(plugin, "plugin为null");
        Preconditions.checkNotNull(plugin.getId(), "id不能为空");
        EntityWrapper<Plugin> entityWrapper = new EntityWrapper();
        entityWrapper.eq("id", plugin.getId());
        pluginService.deleteDbAndZk(entityWrapper, plugin);
        return Mono.just(new ResponseEntity<>().success());
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Mono<ResponseEntity> update(@RequestBody Plugin plugin) {
        Preconditions.checkNotNull(plugin, "plugin为null");
        Preconditions.checkNotNull(plugin.getId(), "id不能为空");
        Preconditions.checkArgument(StringUtils.isNoneBlank(plugin.getName()));
        Preconditions.checkArgument(StringUtils.contains("01", plugin.getRole().toString())
                && StringUtils.contains("01", plugin.getEnabled().toString()));
        plugin.setDateUpdated(new Timestamp(System.currentTimeMillis()));
        EntityWrapper<Plugin> entityWrapper = new EntityWrapper();
        entityWrapper.eq("id", plugin.getId());
        pluginService.updateDbAndZk(plugin, entityWrapper);
        return Mono.just(new ResponseEntity<>().success());
    }

    @RequestMapping(value = "switchOpen", method = RequestMethod.POST)
    public Mono<ResponseEntity> switchOpen(@RequestBody Plugin plugin) {
        Preconditions.checkNotNull(plugin, "plugin为null");
        Preconditions.checkNotNull(plugin.getId(), "id不能为空");
        EntityWrapper<Plugin> entityWrapper = new EntityWrapper();
        entityWrapper.eq("id", plugin.getId());
        plugin.setEnabled(1 - plugin.getEnabled());
        pluginService.updateDbAndZk(plugin, entityWrapper);
        return Mono.just(new ResponseEntity<>().success());
    }

    @RequestMapping(value = "sync", method = RequestMethod.GET)
    public Mono<ResponseEntity> sync() {
        List<Plugin> list = pluginService.selectList(new EntityWrapper());
        pluginService.removeAllZkDataByParentPath(ZkPathConstants.PLUGIN_PARENT_PATH);
        list.stream().forEach(p -> pluginService.insertPluginDataToZk(p));
        return Mono.just(new ResponseEntity().success());
    }

}

